from PySide6.QtWidgets import QWidget,QVBoxLayout,QGridLayout,QSizePolicy,QScrollArea,QPushButton
from PySide6.QtCore import *

class DialogAttribute(QWidget):
    """属性选择对话框 - 带有彩色按钮"""
    Signal_attribute = Signal(str)
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName("attributeDialog")
        self.setup_ui()
        self.connect_init()
        self.apply_button_colors()
        
    def setup_ui(self):
        """设置UI布局"""
        # 创建主布局
        main_layout = QVBoxLayout(self)
        main_layout.setContentsMargins(0, 0, 0, 0)
        main_layout.setSpacing(0)
        
        # 创建网格布局放置属性按钮
        grid_layout = QGridLayout()
        grid_layout.setHorizontalSpacing(15)
        grid_layout.setVerticalSpacing(15)
        
        # 属性按钮列表
        self.attributes = [
            "草系", "火系", "水系", "光系", "土系", "石系", "冰系", "龙系", "电系", "毒系",
            "虫系", "武系", "翼系", "萌系", "幽灵系", "恶魔系", "机械系", "普通系", 
            "神火系", "神草系", "神水系"
        ]
        
        # 添加属性按钮
        for i, attr in enumerate(self.attributes):
            row = i // 3
            col = i % 3
            btn = QPushButton(attr, self)
            # 修改尺寸策略：垂直方向可扩展
            btn.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.MinimumExpanding)  # 修改这里
            btn.setObjectName(f"pb_attribute_{i+1}")
            grid_layout.addWidget(btn, row, col)
        
        # 添加取消按钮（放在最后一行）
        self.cancel_btn = QPushButton("清空", self)
        self.cancel_btn.setObjectName("pb_cancel")
        self.cancel_btn.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.MinimumExpanding)
        # 放在最后一行，跨3列
        last_row = grid_layout.rowCount()
        grid_layout.addWidget(self.cancel_btn, last_row, 1, 1, 2)

        # 为所有行设置拉伸因子（使每行高度均匀分配空间）
        for row in range(grid_layout.rowCount()):
            grid_layout.setRowStretch(row, 1)  # 添加这行

        # 创建滚动区域
        scroll_area = QScrollArea()
        scroll_area.setWidgetResizable(True)
        scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        
        # 创建容器承载网格
        container = QWidget()
        container.setLayout(grid_layout)
        scroll_area.setWidget(container)
        
        # 关键：设置滚动区域的尺寸策略
        scroll_area.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        
        main_layout.addWidget(scroll_area)
        
    
    def apply_button_colors(self):
        """应用按钮颜色设置"""
        attribute_map = {
            "草系": "grass",
            "火系": "fire",
            "水系": "water",
            "光系": "light",
            "土系": "earth",
            "石系": "stone",
            "冰系": "ice",
            "龙系": "dragon",
            "电系": "electric",
            "毒系": "poison",
            "虫系": "bug",
            "武系": "fighting",
            "翼系": "flying",
            "萌系": "cute",
            "幽灵系": "ghost",
            "恶魔系": "demon",
            "机械系": "mechanical",
            "普通系": "normal",
            "神火系": "god_fire",
            "神草系": "god_grass",
            "神水系": "god_water",
            "无": "none"
        } 
        
        # 为每个属性按钮设置背景色
        for i, attr in enumerate(self.attributes):
            btn = self.findChild(QPushButton, f"pb_attribute_{i+1}")
            if btn:
                # 清除原有样式
                btn.setStyleSheet("")
                
                # 设置类属性（与QLabel使用相同的类名）
                class_name = attribute_map.get(attr, "")
                if class_name:
                    btn.setProperty("class", class_name)
                
                # 强制更新样式
                btn.style().unpolish(btn)
                btn.style().polish(btn)
                btn.update()
    
    def connect_init(self):
        """连接信号与槽"""
        # 连接所有属性按钮
        for i in range(1, len(self.attributes) + 1):
            button = self.findChild(QPushButton, f"pb_attribute_{i}")
            if button:
                button.clicked.connect(self.select_attribute)
        
        # 连接取消按钮
        if self.cancel_btn:
            self.cancel_btn.clicked.connect(self.on_cancel)
    
    def select_attribute(self):
        """处理属性选择"""
        button = self.sender()
        if button:
            attribute_text = button.text()
            self.Signal_attribute.emit(attribute_text)
    
    def on_cancel(self):
        """处理取消操作"""
        self.Signal_attribute.emit("清空")